<?php
@session_start();
header("Content-Type: text/html;charset=utf-8");

include_once '../db_conecta_adodb.inc.php';
include_once '../function.inc.php';

$idBook=$_REQUEST['id'];

$rs=sql("SELECT `T_BOOKS`.`DT_CHECK_IN`,
			    `T_BOOKS`.`DT_CHECK_OUT`,
			    `T_BOOKS`.`INT_GUESTS`,
			    `T_BOOKS`.`CHR_NAME`,
			    `T_BOOKS`.`CHR_LAST_NAME`,
			    `T_BOOKS`.`FK_ID_COUNTRY`,
			    `T_BOOKS`.`CHR_EMAIL`,
			    `T_BOOKS`.`CHR_PHONE`,
			    `T_BOOKS`.`CHR_OPT_PHONE`,
			    `T_BOOKS`.`CHR_ARRIVE`,
			    `T_BOOKS`.`CHR_REQUEST`,
			    `T_BOOKS`.`CHR_BOOK_CODE`,
			    `T_BOOKS`.`DT_ENTRY`,
			    `T_BOOKS`.`DCM_PRICE`,
			    `T_BOOKS`.`DCM_VAT`,
			    `T_BOOKS`.`DCM_TOTAL`,
			    `T_BOOKS`.`ID_CURRENCY`,
			    `T_BOOKS`.`DT_EXPIRATION_DATE`,
			    `T_BOOKS`.`INT_DAYS`,
			    `T_BOOKS`.`INT_DEPOSIT`,
			    `T_BOOKS`.`DCM_DEPOSIT_VALUE`,
			    `T_BOOKS`.`DT_DEPOSIT_DATE`,
			    `T_BOOKS`.`ID_DEPOSIT_CURRENCY`,
			    `T_BOOKS`.`INT_ARRIVE_VALUE`,
			    `T_BOOKS`.`INT_CANCEL`,
			    `T_BOOKS`.`DT_CANCEL`,
			    `T_BOOKS`.`INT_TOT_PAY`,
			    `T_BOOKS`.`DT_TOT_PAY`,
			    `T_BOOKS`.`CHR_COMMENTS`,
			    `T_COUNTRIES`.`CHR_NAME_ES`,
			    `T_COUNTRIES`.`CHR_DIAL_CODE`,
			    `T_CURRENCY`.`CHR_VALUE`
			FROM `u172676809_fcac`.`T_BOOKS`,`u172676809_fcac`.`T_COUNTRIES`,`u172676809_fcac`.`T_CURRENCY` WHERE `T_BOOKS`.`FK_ID_COUNTRY`=`T_COUNTRIES`.`ID_COUNTRY`
			AND `T_BOOKS`.`ID_CURRENCY`=`T_CURRENCY`.`ID_CURRENCY` AND `T_BOOKS`.`INT_ID_BOOK`=?",array($idBook));
		
$row=siguiente($rs);
$checkin=$row->DT_CHECK_IN;
$checkout=$row->DT_CHECK_OUT;
$checkinV=$row->DT_CHECK_IN;
$checkin = formatDateLong($checkin,'es');
$checkout = formatDateLong($checkout,'es');
$guests=$row->INT_GUESTS;
$days=$row->INT_DAYS;
$arrive=$row->CHR_ARRIVE;
$request=$row->CHR_REQUEST;
$name=$row->CHR_NAME;
$lastName=$row->CHR_LAST_NAME;
$email=$row->CHR_EMAIL;
$phone=$row->CHR_PHONE;
$optPhone=$row->CHR_OPT_PHONE;
$countryName=$row->CHR_NAME_ES;
$dialCode=$row->CHR_DIAL_CODE;
$cancel=$row->INT_CANCEL;
$dateCancel=$row->DT_CANCEL;
$expDate=$row->DT_EXPIRATION_DATE;
$deposit=$row->INT_DEPOSIT;
$totPay=$row->INT_TOT_PAY;
$total=$row->DCM_TOTAL;
$valCurrency=$row->CHR_VALUE;
$price=$row->DCM_PRICE;
$vat=$row->DCM_VAT;
$depositValue=$row->DCM_DEPOSIT_VALUE;
$depositDate=$row->DT_DEPOSIT_DATE;
$depositCurrency=$row->ID_DEPOSIT_CURRENCY;
$dateTotPay=$row->DT_TOT_PAY;
$observaciones=$row->CHR_COMMENTS;
$fechaAlta=$row->DT_ENTRY;

$array=getEstado($row->DT_CHECK_OUT,$cancel,$dateCancel,$expDate,$deposit,$totPay);
//var_dump($array->estado);
$estado=$array['estado'];
$archivo=$array['archivo'];
$title=$array['title'];

//Busco la cotizacion de la fecha de alta de la reserva
$rate=getDateRate($idBook);
?>
<script type="text/javascript">
	$(document).ready(function(){
		$("#inp-fecha-vencimiento").datepicker({
			showOn: "both",
			dateFormat: "dd/mm/yy",
			buttonImage: "../img/symbol/calendar-icon-512x512.png",
			maxDate: "<?php echo dateFromDBtoPick($checkinV,'es');?>",
			minDate: "<?php echo dateFromDBtoPick($expDate,'es');?>"
			
		});

		$('.tooltip').tooltipster({
			contentAsHTML: true,
			animation: 'grow'
		});

		//Cargo combo moneda
		g('../ajax.php?'+a('ArmarComboMoneda')+c('table','T_CURRENCY')+c('id','id_currency')+c('value','chr_value')+c('name','inpCurrDeposit')+c('puntero',$('#curr').val())+c('disable',$('#disabled').val()), 'SelectCurrDeposit' ,'div-msg', 'div-img' , true);
	  	
	  	//Deposito
	  	$('#btn-deposit').click(function() {
			g('ajax_tabla_reservas.php?'+a('validateDeposit')+v('id')+v('inp-Deposit')+v("inpCurrDeposit"),'div-result','div-msg','div-img',false);
		});

		$("#btn-cancel-yes").on("click",function(e){
			g("ajax_tabla_reservas.php?"+a("setDeposit")+v('id')+v("inp-Deposit")+v("inpCurrDeposit"), "div-result" , "div-msg", "div-img", false);
			$("#modalDepositMin").modal("toggle");
			
		});
		
		//Saldo
		$("#btn-reg-saldo").on("click",function(e){
			g("ajax_tabla_reservas.php?"+a("setSaldo")+v('id')+c("cancel","0"), "div-result" , "div-msg", "div-img" ,false);
		});
		$("#btn-canc-saldo").on("click",function(e){
			g("ajax_tabla_reservas.php?"+a("setSaldo")+v('id')+c("cancel","1"), "div-result" , "div-msg", "div-img" ,false);
		});

		//Prorrogar Fecha Vencimiento
		$("#btn-pro-venc").on("click",function(e){
			g("ajax_tabla_reservas.php?"+a("prorrogarVenc")+v('id')+v('inp-fecha-vencimiento'), "div-result" , "div-msg", "div-img" ,false);
		});

		//Cancelar
		$('#btn-cancel-book').click(function() {
			$('#modalCancelarReserva').modal('toggle');
		});
		$('#btn-canc-yes').click(function() {
			g('ajax_tabla_reservas.php?'+a('setCancel')+v('id')+v('inp-motivo-cancel'),'div-result','div-msg','div-img',true);
			$('#modalCancelarReserva').modal('toggle');
		});

		//Observaciones
		$('#btn-observaciones').click(function() {
			g('ajax_tabla_reservas.php?'+a('setObservacion')+v('id')+v('inp-observaciones'),'div-result','div-msg','div-img',true);
		});

		$("#return-tabla").on("click",function(e){
			var table = $('#tabla').dataTable();
			table.fnReloadAjax();
			$('#adm-reserva').toggle(300);
			$('#principal').toggle(300);
			$('#adm-reserva').html("");

			if (isSet('div-msg'))
					$('#div-msg').fadeOut();
				
		});
		
	});
</script>

<div class="panel panel-default">
	<div class="panel-heading">Datos Reserva</div>
	<div class="panel-body">
		<div class="row" style="background-color:#F0FF9E;">
			<div class="col-xs-4 col-xs-offset-4">
				<span style="font-size:1.5em;">Estado :<img class="tooltip" src="../img/panel/<?php echo $archivo;?>.png"/><strong><?php echo $title;?></strong></span>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-4">
				<span>Entrada: <strong><?php echo $checkin;?></strong></span>
			</div>
			<div class="col-xs-4">
				<span>Salida: <strong><?php echo $checkout;?></strong></span>
			</div>
			<div class="col-xs-4">
				<span>Noches: <strong><?php echo $days;?></strong></span>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-4">
				<span>Hu&eacute;spedes: <strong><?php echo $guests;?></strong></span>
			</div>
			<div class="col-xs-4">
				<span>Horario Estimado de Llegada: <strong><?php echo $arrive;?></strong></span>
			</div>
			<div class="col-xs-4">
				<span>Peticiones Especiales: <strong><?php echo $request?></strong></span>
			</div>
		</div>
		<div class="row">
			<hr style="border-top: 1px solid #BDBDBD;margin:0.3em;">
		</div>
		<div class="row">
			<div class="col-xs-4">
				<span>Nombre: <strong><?php echo $name;?></strong></span>
			</div>
			<div class="col-xs-4">
				<span>Apellido: <strong><?php echo $lastName;?></strong></span>
			</div>
			<div class="col-xs-4">
				<span>Pa&iacute;s: <strong><?php echo $countryName;?></strong></span>
			</div>
		</div>
		<div class="row">
			<div class="col-xs-4">
				<span>EMail: <strong><?php echo $email;?></strong></span>
			</div>
			<div class="col-xs-4">
				<span>Tel&eacute;fono: <strong><?php echo $dialCode.' '.$phone;?></strong></span>
			</div>
			<div class="col-xs-4">
				<span>Tel&eacute;fono Alt: <strong><?php echo $dialCode.' '.$optPhone;?></strong></span>
			</div>
		</div>
		<div class="row">
			<hr style="border-top: 1px solid #BDBDBD;margin:0.3em;">
		</div>
		<div class="row">
			<div class="col-xs-4">
				<div class="panel panel-success">
					<div class="panel-body">
						<div class="col-xs-3">
							<img style="cursor:pointer;" class="tooltip tooltipstered" src="../img/panel/coin-us-dollar-icon.png" onmouseover="this.src='../img/panel/coin-us-dollar-icon-active.png'" onmouseout="this.src='../img/panel/coin-us-dollar-icon.png'" title="<?php echo $rate;?>"/></br>
						</div>
						<div class="col-xs-9">
							<span>Precio: <strong><?php echo $valCurrency.' '.$price;?></strong></span></br>
							<span>I.V.A.: <strong><?php echo $valCurrency.' '.$vat;?></strong></span></br>
							<span>Total: <strong><?php echo $valCurrency.' '.$total;?></strong></span>
						</div>
					</div>
				</div>
			</div>
			<div class="col-xs-5">
				<div class="panel panel-info">
					<div class="panel-body">
						<span>Fecha de Alta:<strong> <?php echo formatDateLong($fechaAlta,'es');?></strong></span></br>
						<span>Fecha de Vencimiento:<strong>  <?php echo formatDateLong($expDate,'es');?></strong> </span>
					</div>
				</div>
			</div>
			<div class="col-xs-3">
				
			</div>
		</div>
	</div>
</div>

<div class="panel panel-default">
	<div class="panel-heading">Administrar Reserva</div>
	<div class="panel-body">
		<?php
		if ($estado=='depositada'||$estado=='pendiente'||$estado=='liquidada'){
			//Deposito
			$porc_min_deposit=getParamGral('PORC_MIN_DEPOSITO');
			$minDeposito=calcMinDeposit($total);

			if ($estado=='pendiente'){
				$img='../img/symbol/pendiente.png';
				$title='Pendiente';
				$curr='3';
				$btn='Registrar Dep&oacute;sito';
			}else{
				$img='../img/symbol/ok.png';
				$curr=$depositCurrency;
				$title='Dep&oacute;sito realizado el '.formatDateLong($depositDate,'es');
				$btn='Modificar Dep&oacute;sito';
			}

			//Si esta liquidada no se puede editar el deposito
			if ($estado=='liquidada'){
				$disable='1';
				$input='disabled="disabled"';
			}else{
				$disable='0';
				$input='';
			}
		?>
		<div class="row" style="margin-top:1em;">
			<div class="col-xs-2">
				<button id="btn-deposit" type="button" class="btn btn-success" style="width: 100%; "<?php echo $input;?>><?php echo $btn;?></button>
			</div>
			<div class="col-xs-1"><img src="<?php echo $img;?>" title="<?php echo $title;?>" class="tooltip"></div>
			<div class="col-xs-2">
				<div id="SelectCurrDeposit"></div>
			</div>
			<div class="col-xs-2">
				<input type="text" id="inp-Deposit" value="<?php echo $depositValue;?>"<?php echo $input;?>/>
			</div>
			
			<div class="col-xs-4">
				<img src="../img/symbol/symbol_information.png" class="img-info" style="width:2em;"/>
				<span style="font-size:1.2em;">Dep&oacute;sito M&iacute;nimo(<?php echo $porc_min_deposit;?>%): <strong><?php echo $valCurrency.' '.$minDeposito;?></strong></span>
			</div>
		</div>
		<?php
		//Pago Saldo
		if ($estado=='liquidada'){
				$img='../img/symbol/ok.png';
				$title='Saldo Abonado el '.formatDateLong($dateTotPay,'es');
				$btn='Cancelar Pago Saldo';
				$idBtn="btn-canc-saldo";
				$class='danger';
				$saldo=0;
		}else{
				$img='../img/symbol/pendiente.png';
				$title='Pendiente';
				$btn='Registrar Pago Saldo';
				$idBtn="btn-reg-saldo";
				$class='success';
				$saldo=round(calcSaldo($idBook),2);
		}
		?>
		<div class="row" style="margin-top:1em;">
			<div class="col-xs-2">
				<button id="<?php echo $idBtn;?>" type="button" class="btn btn-<?php echo $class;?>" style="width: 100%;"><?php echo $btn;?></button>
			</div>
			<div class="col-xs-1">
				<img src="<?php echo $img;?>" title="<?php echo $title;?>" class="tooltip">
			</div>
			<div class="col-xs-4 col-xs-offset-4">
				<img src="../img/symbol/symbol_information.png" class="img-info" style="width:2em;"/>
				<span style="font-size:1.2em;">Saldo Pendiente: <strong><?php echo $valCurrency.' '.$saldo;?></strong></span>
			</div>
		</div>
		<div class="row" style="margin-top:1em;">
			<div class="col-xs-2">
				<button id="btn-cancel-book" type="button" class="btn btn-danger" style="width: 100%;">Cancelar Reserva</button>
			</div>
			<div class="col-xs-4">
				<textarea placeholder="Motivos por los que se cancela la reserva" type="text" id="inp-motivo-cancel" style="width: 100%;"></textarea>
			</div>
		</div>
		<?php }
		//Si esta liquidada, deposita, pendiente o vendida y la fecha de checkin es mayor a la actual
		//Prorrogar fecha de vencimiento
		if ($estado=='depositada'||$estado=='pendiente'||$estado=='liquidada' || ($estado=='vencida' && date($checkinV) > date('Y-m-d H:i:s'))) {
		?>

		
		<div class="row" style="margin-top:1em;">
			<div class="col-xs-2">
				<button id="btn-pro-venc" type="button" class="btn btn-success" style="width: 100%;">Prorrogar Vencimiento</button>
			</div>
			<div class="col-xs-4">
				<input id="inp-fecha-vencimiento" type="text" value="<?php echo dateFromDBtoPick($expDate,'es');?>">
			</div>
		</div>
		<?php } ?>
		<div class="row" style="margin-top:1em;">
			<div class="col-xs-2">
				<button id="btn-observaciones" type="button" class="btn btn-success" style="width: 100%;">Reg Observaciones</button>
			</div>
			<div class="col-xs-4">
				<textarea type="text" id="inp-observaciones" style="width: 100%;"><?php echo $observaciones;?></textarea>
			</div>
		</div>
		
		<input id="curr" type="hidden" value="<?php echo $curr;?>"/>
		<input id="disabled" type="hidden" value="<?php echo $disable;?>"/>
		<input id="id" type="hidden" value="<?php echo $idBook;?>"/>
		<div class="row margin-top-1em">
			<div class="col-xs-4 col-xs-offset-4">
				<button class="btn btn-default btn-custom" id="return-tabla">
					<!--<img style="display:inline;" src="../img/panel/tabla_reservas.png"/>   -->Volver a Tabla de Reservas</button>
			</div>
		</div>
	</div>
</div>



<!-- Modal: Deposito menor que el minimo -->
<div id="modalDepositMin" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-sm modal-dialog-alert">
    	<div class="modal-content modal-content-alert">
      		<div class="modal-header modal-header-alert">
        		<h2 class="modal-title" id="lblSrv">Dep&oacute;sito Menor que el M&iacute;nimo</h2>
       		</div>
      		<div class="modal-body modal-body-alert">
      			<p>El valor del Dep&oacute;sito ingresado es menor que el m&iacute;nimo aceptado.</p>
      			<p>¿Desea registrarlo igualmente?</p>
			</div>
			<div class="modal-footer modal-footer-alert">
		    	<button id="btn-cancel-yes" type="button" class="btn btn-success btn-custom">Si</button>
		        <button type="button" class="btn btn-danger btn-custom" data-dismiss="modal">No</button>
		    </div>
      	</div>
    </div>
</div>

<!-- Modal: Cancelar Reserva -->
<div id="modalCancelarReserva" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-sm modal-dialog-alert">
    	<div class="modal-content modal-content-alert">
      		<div class="modal-header modal-header-alert">
        		<h2 class="modal-title" id="lblSrv">Cancelar Reserva</h2>
       		</div>
      		<div class="modal-body modal-body-alert">
      			<p>¿Est&aacute; seguro que desea Cancelar la Reserva?</p>
      			<p>Recuerde que NO puede deshacer esta acci&oacute;n</p>
			</div>
			<div class="modal-footer modal-footer-alert">
		    	<button id="btn-canc-yes" type="button" class="btn btn-success btn-custom">Si</button>
		        <button type="button" class="btn btn-danger btn-custom" data-dismiss="modal">No</button>
		    </div>
      	</div>
    </div>
</div>